
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>django.core.management.base &#8212; Django 2.2.12.dev20200304094918 documentation</title>
    <link rel="stylesheet" href="../../../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../../../_static/pygments.css" type="text/css" />
    <script type="text/javascript" id="documentation_options" data-url_root="../../../../" src="../../../../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../../../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../../../_static/doctools.js"></script>
    <script type="text/javascript" src="../../../../_static/language_data.js"></script>
    <link rel="index" title="Index" href="../../../../genindex.html" />
    <link rel="search" title="Search" href="../../../../search.html" />



 
<script type="text/javascript" src="../../../../templatebuiltins.js"></script>
<script type="text/javascript">
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "../../../../ref/templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);</script>

  </head><body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../../../../index.html">Django 2.2.12.dev20200304094918 documentation</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../../../../index.html">Home</a>  |
        <a title="Table of contents" href="../../../../contents.html">Table of contents</a>  |
        <a title="Global index" href="../../../../genindex.html">Index</a>  |
        <a title="Module index" href="../../../../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    <a href="../../../index.html" title="Module code" accesskey="U">up</a></div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="_modules-django-core-management-base">
            
  <h1>Source code for django.core.management.base</h1><div class="highlight"><pre>
<span></span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">Base classes for writing management commands (named commands which can</span>
<span class="sd">be executed through ``django-admin`` or ``manage.py``).</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="kn">import</span> <span class="nn">os</span>
<span class="kn">import</span> <span class="nn">sys</span>
<span class="kn">from</span> <span class="nn">argparse</span> <span class="k">import</span> <span class="n">ArgumentParser</span><span class="p">,</span> <span class="n">HelpFormatter</span>
<span class="kn">from</span> <span class="nn">io</span> <span class="k">import</span> <span class="n">TextIOBase</span>

<span class="kn">import</span> <span class="nn">django</span>
<span class="kn">from</span> <span class="nn">django.core</span> <span class="k">import</span> <span class="n">checks</span>
<span class="kn">from</span> <span class="nn">django.core.exceptions</span> <span class="k">import</span> <span class="n">ImproperlyConfigured</span>
<span class="kn">from</span> <span class="nn">django.core.management.color</span> <span class="k">import</span> <span class="n">color_style</span><span class="p">,</span> <span class="n">no_style</span>
<span class="kn">from</span> <span class="nn">django.db</span> <span class="k">import</span> <span class="n">DEFAULT_DB_ALIAS</span><span class="p">,</span> <span class="n">connections</span>


<div class="viewcode-block" id="CommandError"><a class="viewcode-back" href="../../../../howto/custom-management-commands.html#django.core.management.CommandError">[docs]</a><span class="k">class</span> <span class="nc">CommandError</span><span class="p">(</span><span class="ne">Exception</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Exception class indicating a problem while executing a management</span>
<span class="sd">    command.</span>

<span class="sd">    If this exception is raised during the execution of a management</span>
<span class="sd">    command, it will be caught and turned into a nicely-printed error</span>
<span class="sd">    message to the appropriate output stream (i.e., stderr); as a</span>
<span class="sd">    result, raising this exception (with a sensible description of the</span>
<span class="sd">    error) is the preferred way to indicate that something has gone</span>
<span class="sd">    wrong in the execution of a command.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">pass</span></div>


<span class="k">class</span> <span class="nc">SystemCheckError</span><span class="p">(</span><span class="n">CommandError</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    The system check framework detected unrecoverable errors.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">pass</span>


<span class="k">class</span> <span class="nc">CommandParser</span><span class="p">(</span><span class="n">ArgumentParser</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Customized ArgumentParser class to improve some error messages and prevent</span>
<span class="sd">    SystemExit in several occasions, as SystemExit is unacceptable when a</span>
<span class="sd">    command is called programmatically.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="p">,</span> <span class="n">missing_args_message</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">called_from_command_line</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">missing_args_message</span> <span class="o">=</span> <span class="n">missing_args_message</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">called_from_command_line</span> <span class="o">=</span> <span class="n">called_from_command_line</span>
        <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">parse_args</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">args</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">namespace</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="c1"># Catch missing argument for a better error message</span>
        <span class="k">if</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">missing_args_message</span> <span class="ow">and</span>
                <span class="ow">not</span> <span class="p">(</span><span class="n">args</span> <span class="ow">or</span> <span class="nb">any</span><span class="p">(</span><span class="ow">not</span> <span class="n">arg</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;-&#39;</span><span class="p">)</span> <span class="k">for</span> <span class="n">arg</span> <span class="ow">in</span> <span class="n">args</span><span class="p">))):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">error</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">missing_args_message</span><span class="p">)</span>
        <span class="k">return</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">parse_args</span><span class="p">(</span><span class="n">args</span><span class="p">,</span> <span class="n">namespace</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">error</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">message</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">called_from_command_line</span><span class="p">:</span>
            <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">error</span><span class="p">(</span><span class="n">message</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">CommandError</span><span class="p">(</span><span class="s2">&quot;Error: </span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="n">message</span><span class="p">)</span>


<span class="k">def</span> <span class="nf">handle_default_options</span><span class="p">(</span><span class="n">options</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Include any default options that all commands should accept here</span>
<span class="sd">    so that ManagementUtility can handle them before searching for</span>
<span class="sd">    user commands.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">if</span> <span class="n">options</span><span class="o">.</span><span class="n">settings</span><span class="p">:</span>
        <span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="p">[</span><span class="s1">&#39;DJANGO_SETTINGS_MODULE&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">options</span><span class="o">.</span><span class="n">settings</span>
    <span class="k">if</span> <span class="n">options</span><span class="o">.</span><span class="n">pythonpath</span><span class="p">:</span>
        <span class="n">sys</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">insert</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">options</span><span class="o">.</span><span class="n">pythonpath</span><span class="p">)</span>


<span class="k">def</span> <span class="nf">no_translations</span><span class="p">(</span><span class="n">handle_func</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Decorator that forces a command to run with translations deactivated.&quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="nf">wrapped</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="kn">from</span> <span class="nn">django.utils</span> <span class="k">import</span> <span class="n">translation</span>
        <span class="n">saved_locale</span> <span class="o">=</span> <span class="n">translation</span><span class="o">.</span><span class="n">get_language</span><span class="p">()</span>
        <span class="n">translation</span><span class="o">.</span><span class="n">deactivate_all</span><span class="p">()</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">res</span> <span class="o">=</span> <span class="n">handle_func</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
        <span class="k">finally</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">saved_locale</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
                <span class="n">translation</span><span class="o">.</span><span class="n">activate</span><span class="p">(</span><span class="n">saved_locale</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">res</span>
    <span class="k">return</span> <span class="n">wrapped</span>


<span class="k">class</span> <span class="nc">DjangoHelpFormatter</span><span class="p">(</span><span class="n">HelpFormatter</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Customized formatter so that command-specific arguments appear in the</span>
<span class="sd">    --help output before arguments common to all commands.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">show_last</span> <span class="o">=</span> <span class="p">{</span>
        <span class="s1">&#39;--version&#39;</span><span class="p">,</span> <span class="s1">&#39;--verbosity&#39;</span><span class="p">,</span> <span class="s1">&#39;--traceback&#39;</span><span class="p">,</span> <span class="s1">&#39;--settings&#39;</span><span class="p">,</span> <span class="s1">&#39;--pythonpath&#39;</span><span class="p">,</span>
        <span class="s1">&#39;--no-color&#39;</span><span class="p">,</span> <span class="s1">&#39;--force-color&#39;</span><span class="p">,</span>
    <span class="p">}</span>

    <span class="k">def</span> <span class="nf">_reordered_actions</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">actions</span><span class="p">):</span>
        <span class="k">return</span> <span class="nb">sorted</span><span class="p">(</span>
            <span class="n">actions</span><span class="p">,</span>
            <span class="n">key</span><span class="o">=</span><span class="k">lambda</span> <span class="n">a</span><span class="p">:</span> <span class="nb">set</span><span class="p">(</span><span class="n">a</span><span class="o">.</span><span class="n">option_strings</span><span class="p">)</span> <span class="o">&amp;</span> <span class="bp">self</span><span class="o">.</span><span class="n">show_last</span> <span class="o">!=</span> <span class="nb">set</span><span class="p">()</span>
        <span class="p">)</span>

    <span class="k">def</span> <span class="nf">add_usage</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">usage</span><span class="p">,</span> <span class="n">actions</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">add_usage</span><span class="p">(</span><span class="n">usage</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_reordered_actions</span><span class="p">(</span><span class="n">actions</span><span class="p">),</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">add_arguments</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">actions</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">add_arguments</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_reordered_actions</span><span class="p">(</span><span class="n">actions</span><span class="p">))</span>


<span class="k">class</span> <span class="nc">OutputWrapper</span><span class="p">(</span><span class="n">TextIOBase</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Wrapper around stdout/stderr</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">style_func</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_style_func</span>

    <span class="nd">@style_func</span><span class="o">.</span><span class="n">setter</span>
    <span class="k">def</span> <span class="nf">style_func</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">style_func</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">style_func</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">isatty</span><span class="p">():</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_style_func</span> <span class="o">=</span> <span class="n">style_func</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_style_func</span> <span class="o">=</span> <span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">x</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">out</span><span class="p">,</span> <span class="n">style_func</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">ending</span><span class="o">=</span><span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_out</span> <span class="o">=</span> <span class="n">out</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">style_func</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">ending</span> <span class="o">=</span> <span class="n">ending</span>

    <span class="k">def</span> <span class="nf">__getattr__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">):</span>
        <span class="k">return</span> <span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_out</span><span class="p">,</span> <span class="n">name</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">isatty</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="nb">hasattr</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_out</span><span class="p">,</span> <span class="s1">&#39;isatty&#39;</span><span class="p">)</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">_out</span><span class="o">.</span><span class="n">isatty</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">write</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">msg</span><span class="p">,</span> <span class="n">style_func</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">ending</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="n">ending</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">ending</span> <span class="k">if</span> <span class="n">ending</span> <span class="ow">is</span> <span class="kc">None</span> <span class="k">else</span> <span class="n">ending</span>
        <span class="k">if</span> <span class="n">ending</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">msg</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="n">ending</span><span class="p">):</span>
            <span class="n">msg</span> <span class="o">+=</span> <span class="n">ending</span>
        <span class="n">style_func</span> <span class="o">=</span> <span class="n">style_func</span> <span class="ow">or</span> <span class="bp">self</span><span class="o">.</span><span class="n">style_func</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_out</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">style_func</span><span class="p">(</span><span class="n">msg</span><span class="p">))</span>


<div class="viewcode-block" id="BaseCommand"><a class="viewcode-back" href="../../../../howto/custom-management-commands.html#django.core.management.BaseCommand">[docs]</a><span class="k">class</span> <span class="nc">BaseCommand</span><span class="p">:</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    The base class from which all management commands ultimately</span>
<span class="sd">    derive.</span>

<span class="sd">    Use this class if you want access to all of the mechanisms which</span>
<span class="sd">    parse the command-line arguments and work out what code to call in</span>
<span class="sd">    response; if you don&#39;t need to change any of that behavior,</span>
<span class="sd">    consider using one of the subclasses defined in this file.</span>

<span class="sd">    If you are interested in overriding/customizing various aspects of</span>
<span class="sd">    the command-parsing and -execution behavior, the normal flow works</span>
<span class="sd">    as follows:</span>

<span class="sd">    1. ``django-admin`` or ``manage.py`` loads the command class</span>
<span class="sd">       and calls its ``run_from_argv()`` method.</span>

<span class="sd">    2. The ``run_from_argv()`` method calls ``create_parser()`` to get</span>
<span class="sd">       an ``ArgumentParser`` for the arguments, parses them, performs</span>
<span class="sd">       any environment changes requested by options like</span>
<span class="sd">       ``pythonpath``, and then calls the ``execute()`` method,</span>
<span class="sd">       passing the parsed arguments.</span>

<span class="sd">    3. The ``execute()`` method attempts to carry out the command by</span>
<span class="sd">       calling the ``handle()`` method with the parsed arguments; any</span>
<span class="sd">       output produced by ``handle()`` will be printed to standard</span>
<span class="sd">       output and, if the command is intended to produce a block of</span>
<span class="sd">       SQL statements, will be wrapped in ``BEGIN`` and ``COMMIT``.</span>

<span class="sd">    4. If ``handle()`` or ``execute()`` raised any exception (e.g.</span>
<span class="sd">       ``CommandError``), ``run_from_argv()`` will  instead print an error</span>
<span class="sd">       message to ``stderr``.</span>

<span class="sd">    Thus, the ``handle()`` method is typically the starting point for</span>
<span class="sd">    subclasses; many built-in commands and command types either place</span>
<span class="sd">    all of their logic in ``handle()``, or perform some additional</span>
<span class="sd">    parsing work in ``handle()`` and then delegate from it to more</span>
<span class="sd">    specialized methods as needed.</span>

<span class="sd">    Several attributes affect behavior at various steps along the way:</span>

<span class="sd">    ``help``</span>
<span class="sd">        A short description of the command, which will be printed in</span>
<span class="sd">        help messages.</span>

<span class="sd">    ``output_transaction``</span>
<span class="sd">        A boolean indicating whether the command outputs SQL</span>
<span class="sd">        statements; if ``True``, the output will automatically be</span>
<span class="sd">        wrapped with ``BEGIN;`` and ``COMMIT;``. Default value is</span>
<span class="sd">        ``False``.</span>

<span class="sd">    ``requires_migrations_checks``</span>
<span class="sd">        A boolean; if ``True``, the command prints a warning if the set of</span>
<span class="sd">        migrations on disk don&#39;t match the migrations in the database.</span>

<span class="sd">    ``requires_system_checks``</span>
<span class="sd">        A boolean; if ``True``, entire Django project will be checked for errors</span>
<span class="sd">        prior to executing the command. Default value is ``True``.</span>
<span class="sd">        To validate an individual application&#39;s models</span>
<span class="sd">        rather than all applications&#39; models, call</span>
<span class="sd">        ``self.check(app_configs)`` from ``handle()``, where ``app_configs``</span>
<span class="sd">        is the list of application&#39;s configuration provided by the</span>
<span class="sd">        app registry.</span>

<span class="sd">    ``stealth_options``</span>
<span class="sd">        A tuple of any options the command uses which aren&#39;t defined by the</span>
<span class="sd">        argument parser.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="c1"># Metadata about this command.</span>
    <span class="n">help</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span>

    <span class="c1"># Configuration shortcuts that alter various logic.</span>
    <span class="n">_called_from_command_line</span> <span class="o">=</span> <span class="kc">False</span>
    <span class="n">output_transaction</span> <span class="o">=</span> <span class="kc">False</span>  <span class="c1"># Whether to wrap the output in a &quot;BEGIN; COMMIT;&quot;</span>
    <span class="n">requires_migrations_checks</span> <span class="o">=</span> <span class="kc">False</span>
    <span class="n">requires_system_checks</span> <span class="o">=</span> <span class="kc">True</span>
    <span class="c1"># Arguments, common to all commands, which aren&#39;t defined by the argument</span>
    <span class="c1"># parser.</span>
    <span class="n">base_stealth_options</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;skip_checks&#39;</span><span class="p">,</span> <span class="s1">&#39;stderr&#39;</span><span class="p">,</span> <span class="s1">&#39;stdout&#39;</span><span class="p">)</span>
    <span class="c1"># Command-specific options not defined by the argument parser.</span>
    <span class="n">stealth_options</span> <span class="o">=</span> <span class="p">()</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">stdout</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">stderr</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">no_color</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">force_color</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">stdout</span> <span class="o">=</span> <span class="n">OutputWrapper</span><span class="p">(</span><span class="n">stdout</span> <span class="ow">or</span> <span class="n">sys</span><span class="o">.</span><span class="n">stdout</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">stderr</span> <span class="o">=</span> <span class="n">OutputWrapper</span><span class="p">(</span><span class="n">stderr</span> <span class="ow">or</span> <span class="n">sys</span><span class="o">.</span><span class="n">stderr</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">no_color</span> <span class="ow">and</span> <span class="n">force_color</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">CommandError</span><span class="p">(</span><span class="s2">&quot;&#39;no_color&#39; and &#39;force_color&#39; can&#39;t be used together.&quot;</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">no_color</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">style</span> <span class="o">=</span> <span class="n">no_style</span><span class="p">()</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">style</span> <span class="o">=</span> <span class="n">color_style</span><span class="p">(</span><span class="n">force_color</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">stderr</span><span class="o">.</span><span class="n">style_func</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">style</span><span class="o">.</span><span class="n">ERROR</span>

<div class="viewcode-block" id="BaseCommand.get_version"><a class="viewcode-back" href="../../../../howto/custom-management-commands.html#django.core.management.BaseCommand.get_version">[docs]</a>    <span class="k">def</span> <span class="nf">get_version</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Return the Django version, which should be correct for all built-in</span>
<span class="sd">        Django commands. User-supplied commands can override this method to</span>
<span class="sd">        return their own version.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">django</span><span class="o">.</span><span class="n">get_version</span><span class="p">()</span></div>

<div class="viewcode-block" id="BaseCommand.create_parser"><a class="viewcode-back" href="../../../../howto/custom-management-commands.html#django.core.management.BaseCommand.create_parser">[docs]</a>    <span class="k">def</span> <span class="nf">create_parser</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">prog_name</span><span class="p">,</span> <span class="n">subcommand</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Create and return the ``ArgumentParser`` which will be used to</span>
<span class="sd">        parse the arguments to this command.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">parser</span> <span class="o">=</span> <span class="n">CommandParser</span><span class="p">(</span>
            <span class="n">prog</span><span class="o">=</span><span class="s1">&#39;</span><span class="si">%s</span><span class="s1"> </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">basename</span><span class="p">(</span><span class="n">prog_name</span><span class="p">),</span> <span class="n">subcommand</span><span class="p">),</span>
            <span class="n">description</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">help</span> <span class="ow">or</span> <span class="kc">None</span><span class="p">,</span>
            <span class="n">formatter_class</span><span class="o">=</span><span class="n">DjangoHelpFormatter</span><span class="p">,</span>
            <span class="n">missing_args_message</span><span class="o">=</span><span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="s1">&#39;missing_args_message&#39;</span><span class="p">,</span> <span class="kc">None</span><span class="p">),</span>
            <span class="n">called_from_command_line</span><span class="o">=</span><span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="s1">&#39;_called_from_command_line&#39;</span><span class="p">,</span> <span class="kc">None</span><span class="p">),</span>
            <span class="o">**</span><span class="n">kwargs</span>
        <span class="p">)</span>
        <span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s1">&#39;--version&#39;</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s1">&#39;version&#39;</span><span class="p">,</span> <span class="n">version</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">get_version</span><span class="p">())</span>
        <span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span>
            <span class="s1">&#39;-v&#39;</span><span class="p">,</span> <span class="s1">&#39;--verbosity&#39;</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span>
            <span class="nb">type</span><span class="o">=</span><span class="nb">int</span><span class="p">,</span> <span class="n">choices</span><span class="o">=</span><span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">],</span>
            <span class="n">help</span><span class="o">=</span><span class="s1">&#39;Verbosity level; 0=minimal output, 1=normal output, 2=verbose output, 3=very verbose output&#39;</span><span class="p">,</span>
        <span class="p">)</span>
        <span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span>
            <span class="s1">&#39;--settings&#39;</span><span class="p">,</span>
            <span class="n">help</span><span class="o">=</span><span class="p">(</span>
                <span class="s1">&#39;The Python path to a settings module, e.g. &#39;</span>
                <span class="s1">&#39;&quot;myproject.settings.main&quot;. If this isn</span><span class="se">\&#39;</span><span class="s1">t provided, the &#39;</span>
                <span class="s1">&#39;DJANGO_SETTINGS_MODULE environment variable will be used.&#39;</span>
            <span class="p">),</span>
        <span class="p">)</span>
        <span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span>
            <span class="s1">&#39;--pythonpath&#39;</span><span class="p">,</span>
            <span class="n">help</span><span class="o">=</span><span class="s1">&#39;A directory to add to the Python path, e.g. &quot;/home/djangoprojects/myproject&quot;.&#39;</span><span class="p">,</span>
        <span class="p">)</span>
        <span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s1">&#39;--traceback&#39;</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s1">&#39;store_true&#39;</span><span class="p">,</span> <span class="n">help</span><span class="o">=</span><span class="s1">&#39;Raise on CommandError exceptions&#39;</span><span class="p">)</span>
        <span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span>
            <span class="s1">&#39;--no-color&#39;</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s1">&#39;store_true&#39;</span><span class="p">,</span>
            <span class="n">help</span><span class="o">=</span><span class="s2">&quot;Don&#39;t colorize the command output.&quot;</span><span class="p">,</span>
        <span class="p">)</span>
        <span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span>
            <span class="s1">&#39;--force-color&#39;</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s1">&#39;store_true&#39;</span><span class="p">,</span>
            <span class="n">help</span><span class="o">=</span><span class="s1">&#39;Force colorization of the command output.&#39;</span><span class="p">,</span>
        <span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">add_arguments</span><span class="p">(</span><span class="n">parser</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">parser</span></div>

<div class="viewcode-block" id="BaseCommand.add_arguments"><a class="viewcode-back" href="../../../../howto/custom-management-commands.html#django.core.management.BaseCommand.add_arguments">[docs]</a>    <span class="k">def</span> <span class="nf">add_arguments</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">parser</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Entry point for subclassed commands to add custom arguments.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">pass</span></div>

    <span class="k">def</span> <span class="nf">print_help</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">prog_name</span><span class="p">,</span> <span class="n">subcommand</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Print the help message for this command, derived from</span>
<span class="sd">        ``self.usage()``.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">parser</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">create_parser</span><span class="p">(</span><span class="n">prog_name</span><span class="p">,</span> <span class="n">subcommand</span><span class="p">)</span>
        <span class="n">parser</span><span class="o">.</span><span class="n">print_help</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">run_from_argv</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">argv</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Set up any environment changes requested (e.g., Python path</span>
<span class="sd">        and Django settings), then run this command. If the</span>
<span class="sd">        command raises a ``CommandError``, intercept it and print it sensibly</span>
<span class="sd">        to stderr. If the ``--traceback`` option is present or the raised</span>
<span class="sd">        ``Exception`` is not ``CommandError``, raise it.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_called_from_command_line</span> <span class="o">=</span> <span class="kc">True</span>
        <span class="n">parser</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">create_parser</span><span class="p">(</span><span class="n">argv</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">argv</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>

        <span class="n">options</span> <span class="o">=</span> <span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">(</span><span class="n">argv</span><span class="p">[</span><span class="mi">2</span><span class="p">:])</span>
        <span class="n">cmd_options</span> <span class="o">=</span> <span class="nb">vars</span><span class="p">(</span><span class="n">options</span><span class="p">)</span>
        <span class="c1"># Move positional args out of options to mimic legacy optparse</span>
        <span class="n">args</span> <span class="o">=</span> <span class="n">cmd_options</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="s1">&#39;args&#39;</span><span class="p">,</span> <span class="p">())</span>
        <span class="n">handle_default_options</span><span class="p">(</span><span class="n">options</span><span class="p">)</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">cmd_options</span><span class="p">)</span>
        <span class="k">except</span> <span class="ne">Exception</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">options</span><span class="o">.</span><span class="n">traceback</span> <span class="ow">or</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">e</span><span class="p">,</span> <span class="n">CommandError</span><span class="p">):</span>
                <span class="k">raise</span>

            <span class="c1"># SystemCheckError takes care of its own formatting.</span>
            <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">e</span><span class="p">,</span> <span class="n">SystemCheckError</span><span class="p">):</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">stderr</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">e</span><span class="p">),</span> <span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">x</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">stderr</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">%s</span><span class="s1">: </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">e</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span> <span class="n">e</span><span class="p">))</span>
            <span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
        <span class="k">finally</span><span class="p">:</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">connections</span><span class="o">.</span><span class="n">close_all</span><span class="p">()</span>
            <span class="k">except</span> <span class="n">ImproperlyConfigured</span><span class="p">:</span>
                <span class="c1"># Ignore if connections aren&#39;t setup at this point (e.g. no</span>
                <span class="c1"># configured settings).</span>
                <span class="k">pass</span>

<div class="viewcode-block" id="BaseCommand.execute"><a class="viewcode-back" href="../../../../howto/custom-management-commands.html#django.core.management.BaseCommand.execute">[docs]</a>    <span class="k">def</span> <span class="nf">execute</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">options</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Try to execute this command, performing system checks if needed (as</span>
<span class="sd">        controlled by the ``requires_system_checks`` attribute, except if</span>
<span class="sd">        force-skipped).</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">options</span><span class="p">[</span><span class="s1">&#39;force_color&#39;</span><span class="p">]</span> <span class="ow">and</span> <span class="n">options</span><span class="p">[</span><span class="s1">&#39;no_color&#39;</span><span class="p">]:</span>
            <span class="k">raise</span> <span class="n">CommandError</span><span class="p">(</span><span class="s2">&quot;The --no-color and --force-color options can&#39;t be used together.&quot;</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">options</span><span class="p">[</span><span class="s1">&#39;force_color&#39;</span><span class="p">]:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">style</span> <span class="o">=</span> <span class="n">color_style</span><span class="p">(</span><span class="n">force_color</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
        <span class="k">elif</span> <span class="n">options</span><span class="p">[</span><span class="s1">&#39;no_color&#39;</span><span class="p">]:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">style</span> <span class="o">=</span> <span class="n">no_style</span><span class="p">()</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">stderr</span><span class="o">.</span><span class="n">style_func</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="k">if</span> <span class="n">options</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;stdout&#39;</span><span class="p">):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">stdout</span> <span class="o">=</span> <span class="n">OutputWrapper</span><span class="p">(</span><span class="n">options</span><span class="p">[</span><span class="s1">&#39;stdout&#39;</span><span class="p">])</span>
        <span class="k">if</span> <span class="n">options</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;stderr&#39;</span><span class="p">):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">stderr</span> <span class="o">=</span> <span class="n">OutputWrapper</span><span class="p">(</span><span class="n">options</span><span class="p">[</span><span class="s1">&#39;stderr&#39;</span><span class="p">],</span> <span class="bp">self</span><span class="o">.</span><span class="n">stderr</span><span class="o">.</span><span class="n">style_func</span><span class="p">)</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">requires_system_checks</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">options</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;skip_checks&#39;</span><span class="p">):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">check</span><span class="p">()</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">requires_migrations_checks</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">check_migrations</span><span class="p">()</span>
        <span class="n">output</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">handle</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">options</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">output</span><span class="p">:</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">output_transaction</span><span class="p">:</span>
                <span class="n">connection</span> <span class="o">=</span> <span class="n">connections</span><span class="p">[</span><span class="n">options</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;database&#39;</span><span class="p">,</span> <span class="n">DEFAULT_DB_ALIAS</span><span class="p">)]</span>
                <span class="n">output</span> <span class="o">=</span> <span class="s1">&#39;</span><span class="si">%s</span><span class="se">\n</span><span class="si">%s</span><span class="se">\n</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">style</span><span class="o">.</span><span class="n">SQL_KEYWORD</span><span class="p">(</span><span class="n">connection</span><span class="o">.</span><span class="n">ops</span><span class="o">.</span><span class="n">start_transaction_sql</span><span class="p">()),</span>
                    <span class="n">output</span><span class="p">,</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">style</span><span class="o">.</span><span class="n">SQL_KEYWORD</span><span class="p">(</span><span class="n">connection</span><span class="o">.</span><span class="n">ops</span><span class="o">.</span><span class="n">end_transaction_sql</span><span class="p">()),</span>
                <span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">stdout</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">output</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">output</span></div>

    <span class="k">def</span> <span class="nf">_run_checks</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">checks</span><span class="o">.</span><span class="n">run_checks</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>

<div class="viewcode-block" id="BaseCommand.check"><a class="viewcode-back" href="../../../../howto/custom-management-commands.html#django.core.management.BaseCommand.check">[docs]</a>    <span class="k">def</span> <span class="nf">check</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">app_configs</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">tags</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">display_num_errors</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
              <span class="n">include_deployment_checks</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">fail_level</span><span class="o">=</span><span class="n">checks</span><span class="o">.</span><span class="n">ERROR</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Use the system check framework to validate entire Django project.</span>
<span class="sd">        Raise CommandError for any serious message (error or critical errors).</span>
<span class="sd">        If there are only light messages (like warnings), print them to stderr</span>
<span class="sd">        and don&#39;t raise an exception.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">all_issues</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_run_checks</span><span class="p">(</span>
            <span class="n">app_configs</span><span class="o">=</span><span class="n">app_configs</span><span class="p">,</span>
            <span class="n">tags</span><span class="o">=</span><span class="n">tags</span><span class="p">,</span>
            <span class="n">include_deployment_checks</span><span class="o">=</span><span class="n">include_deployment_checks</span><span class="p">,</span>
        <span class="p">)</span>

        <span class="n">header</span><span class="p">,</span> <span class="n">body</span><span class="p">,</span> <span class="n">footer</span> <span class="o">=</span> <span class="s2">&quot;&quot;</span><span class="p">,</span> <span class="s2">&quot;&quot;</span><span class="p">,</span> <span class="s2">&quot;&quot;</span>
        <span class="n">visible_issue_count</span> <span class="o">=</span> <span class="mi">0</span>  <span class="c1"># excludes silenced warnings</span>

        <span class="k">if</span> <span class="n">all_issues</span><span class="p">:</span>
            <span class="n">debugs</span> <span class="o">=</span> <span class="p">[</span><span class="n">e</span> <span class="k">for</span> <span class="n">e</span> <span class="ow">in</span> <span class="n">all_issues</span> <span class="k">if</span> <span class="n">e</span><span class="o">.</span><span class="n">level</span> <span class="o">&lt;</span> <span class="n">checks</span><span class="o">.</span><span class="n">INFO</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">e</span><span class="o">.</span><span class="n">is_silenced</span><span class="p">()]</span>
            <span class="n">infos</span> <span class="o">=</span> <span class="p">[</span><span class="n">e</span> <span class="k">for</span> <span class="n">e</span> <span class="ow">in</span> <span class="n">all_issues</span> <span class="k">if</span> <span class="n">checks</span><span class="o">.</span><span class="n">INFO</span> <span class="o">&lt;=</span> <span class="n">e</span><span class="o">.</span><span class="n">level</span> <span class="o">&lt;</span> <span class="n">checks</span><span class="o">.</span><span class="n">WARNING</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">e</span><span class="o">.</span><span class="n">is_silenced</span><span class="p">()]</span>
            <span class="n">warnings</span> <span class="o">=</span> <span class="p">[</span><span class="n">e</span> <span class="k">for</span> <span class="n">e</span> <span class="ow">in</span> <span class="n">all_issues</span> <span class="k">if</span> <span class="n">checks</span><span class="o">.</span><span class="n">WARNING</span> <span class="o">&lt;=</span> <span class="n">e</span><span class="o">.</span><span class="n">level</span> <span class="o">&lt;</span> <span class="n">checks</span><span class="o">.</span><span class="n">ERROR</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">e</span><span class="o">.</span><span class="n">is_silenced</span><span class="p">()]</span>
            <span class="n">errors</span> <span class="o">=</span> <span class="p">[</span><span class="n">e</span> <span class="k">for</span> <span class="n">e</span> <span class="ow">in</span> <span class="n">all_issues</span> <span class="k">if</span> <span class="n">checks</span><span class="o">.</span><span class="n">ERROR</span> <span class="o">&lt;=</span> <span class="n">e</span><span class="o">.</span><span class="n">level</span> <span class="o">&lt;</span> <span class="n">checks</span><span class="o">.</span><span class="n">CRITICAL</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">e</span><span class="o">.</span><span class="n">is_silenced</span><span class="p">()]</span>
            <span class="n">criticals</span> <span class="o">=</span> <span class="p">[</span><span class="n">e</span> <span class="k">for</span> <span class="n">e</span> <span class="ow">in</span> <span class="n">all_issues</span> <span class="k">if</span> <span class="n">checks</span><span class="o">.</span><span class="n">CRITICAL</span> <span class="o">&lt;=</span> <span class="n">e</span><span class="o">.</span><span class="n">level</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">e</span><span class="o">.</span><span class="n">is_silenced</span><span class="p">()]</span>
            <span class="n">sorted_issues</span> <span class="o">=</span> <span class="p">[</span>
                <span class="p">(</span><span class="n">criticals</span><span class="p">,</span> <span class="s1">&#39;CRITICALS&#39;</span><span class="p">),</span>
                <span class="p">(</span><span class="n">errors</span><span class="p">,</span> <span class="s1">&#39;ERRORS&#39;</span><span class="p">),</span>
                <span class="p">(</span><span class="n">warnings</span><span class="p">,</span> <span class="s1">&#39;WARNINGS&#39;</span><span class="p">),</span>
                <span class="p">(</span><span class="n">infos</span><span class="p">,</span> <span class="s1">&#39;INFOS&#39;</span><span class="p">),</span>
                <span class="p">(</span><span class="n">debugs</span><span class="p">,</span> <span class="s1">&#39;DEBUGS&#39;</span><span class="p">),</span>
            <span class="p">]</span>

            <span class="k">for</span> <span class="n">issues</span><span class="p">,</span> <span class="n">group_name</span> <span class="ow">in</span> <span class="n">sorted_issues</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">issues</span><span class="p">:</span>
                    <span class="n">visible_issue_count</span> <span class="o">+=</span> <span class="nb">len</span><span class="p">(</span><span class="n">issues</span><span class="p">)</span>
                    <span class="n">formatted</span> <span class="o">=</span> <span class="p">(</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">style</span><span class="o">.</span><span class="n">ERROR</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">e</span><span class="p">))</span>
                        <span class="k">if</span> <span class="n">e</span><span class="o">.</span><span class="n">is_serious</span><span class="p">()</span>
                        <span class="k">else</span> <span class="bp">self</span><span class="o">.</span><span class="n">style</span><span class="o">.</span><span class="n">WARNING</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">e</span><span class="p">))</span>
                        <span class="k">for</span> <span class="n">e</span> <span class="ow">in</span> <span class="n">issues</span><span class="p">)</span>
                    <span class="n">formatted</span> <span class="o">=</span> <span class="s2">&quot;</span><span class="se">\n</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="nb">sorted</span><span class="p">(</span><span class="n">formatted</span><span class="p">))</span>
                    <span class="n">body</span> <span class="o">+=</span> <span class="s1">&#39;</span><span class="se">\n</span><span class="si">%s</span><span class="s1">:</span><span class="se">\n</span><span class="si">%s</span><span class="se">\n</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">group_name</span><span class="p">,</span> <span class="n">formatted</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">visible_issue_count</span><span class="p">:</span>
            <span class="n">header</span> <span class="o">=</span> <span class="s2">&quot;System check identified some issues:</span><span class="se">\n</span><span class="s2">&quot;</span>

        <span class="k">if</span> <span class="n">display_num_errors</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">visible_issue_count</span><span class="p">:</span>
                <span class="n">footer</span> <span class="o">+=</span> <span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span>
            <span class="n">footer</span> <span class="o">+=</span> <span class="s2">&quot;System check identified </span><span class="si">%s</span><span class="s2"> (</span><span class="si">%s</span><span class="s2"> silenced).&quot;</span> <span class="o">%</span> <span class="p">(</span>
                <span class="s2">&quot;no issues&quot;</span> <span class="k">if</span> <span class="n">visible_issue_count</span> <span class="o">==</span> <span class="mi">0</span> <span class="k">else</span>
                <span class="s2">&quot;1 issue&quot;</span> <span class="k">if</span> <span class="n">visible_issue_count</span> <span class="o">==</span> <span class="mi">1</span> <span class="k">else</span>
                <span class="s2">&quot;</span><span class="si">%s</span><span class="s2"> issues&quot;</span> <span class="o">%</span> <span class="n">visible_issue_count</span><span class="p">,</span>
                <span class="nb">len</span><span class="p">(</span><span class="n">all_issues</span><span class="p">)</span> <span class="o">-</span> <span class="n">visible_issue_count</span><span class="p">,</span>
            <span class="p">)</span>

        <span class="k">if</span> <span class="nb">any</span><span class="p">(</span><span class="n">e</span><span class="o">.</span><span class="n">is_serious</span><span class="p">(</span><span class="n">fail_level</span><span class="p">)</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">e</span><span class="o">.</span><span class="n">is_silenced</span><span class="p">()</span> <span class="k">for</span> <span class="n">e</span> <span class="ow">in</span> <span class="n">all_issues</span><span class="p">):</span>
            <span class="n">msg</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">style</span><span class="o">.</span><span class="n">ERROR</span><span class="p">(</span><span class="s2">&quot;SystemCheckError: </span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="n">header</span><span class="p">)</span> <span class="o">+</span> <span class="n">body</span> <span class="o">+</span> <span class="n">footer</span>
            <span class="k">raise</span> <span class="n">SystemCheckError</span><span class="p">(</span><span class="n">msg</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">msg</span> <span class="o">=</span> <span class="n">header</span> <span class="o">+</span> <span class="n">body</span> <span class="o">+</span> <span class="n">footer</span>

        <span class="k">if</span> <span class="n">msg</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">visible_issue_count</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">stderr</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">msg</span><span class="p">,</span> <span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">x</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">stdout</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">msg</span><span class="p">)</span></div>

    <span class="k">def</span> <span class="nf">check_migrations</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Print a warning if the set of migrations on disk don&#39;t match the</span>
<span class="sd">        migrations in the database.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="kn">from</span> <span class="nn">django.db.migrations.executor</span> <span class="k">import</span> <span class="n">MigrationExecutor</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">executor</span> <span class="o">=</span> <span class="n">MigrationExecutor</span><span class="p">(</span><span class="n">connections</span><span class="p">[</span><span class="n">DEFAULT_DB_ALIAS</span><span class="p">])</span>
        <span class="k">except</span> <span class="n">ImproperlyConfigured</span><span class="p">:</span>
            <span class="c1"># No databases are configured (or the dummy one)</span>
            <span class="k">return</span>

        <span class="n">plan</span> <span class="o">=</span> <span class="n">executor</span><span class="o">.</span><span class="n">migration_plan</span><span class="p">(</span><span class="n">executor</span><span class="o">.</span><span class="n">loader</span><span class="o">.</span><span class="n">graph</span><span class="o">.</span><span class="n">leaf_nodes</span><span class="p">())</span>
        <span class="k">if</span> <span class="n">plan</span><span class="p">:</span>
            <span class="n">apps_waiting_migration</span> <span class="o">=</span> <span class="nb">sorted</span><span class="p">({</span><span class="n">migration</span><span class="o">.</span><span class="n">app_label</span> <span class="k">for</span> <span class="n">migration</span><span class="p">,</span> <span class="n">backwards</span> <span class="ow">in</span> <span class="n">plan</span><span class="p">})</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">stdout</span><span class="o">.</span><span class="n">write</span><span class="p">(</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">style</span><span class="o">.</span><span class="n">NOTICE</span><span class="p">(</span>
                    <span class="s2">&quot;</span><span class="se">\n</span><span class="s2">You have </span><span class="si">%(unpplied_migration_count)s</span><span class="s2"> unapplied migration(s). &quot;</span>
                    <span class="s2">&quot;Your project may not work properly until you apply the &quot;</span>
                    <span class="s2">&quot;migrations for app(s): </span><span class="si">%(apps_waiting_migration)s</span><span class="s2">.&quot;</span> <span class="o">%</span> <span class="p">{</span>
                        <span class="s2">&quot;unpplied_migration_count&quot;</span><span class="p">:</span> <span class="nb">len</span><span class="p">(</span><span class="n">plan</span><span class="p">),</span>
                        <span class="s2">&quot;apps_waiting_migration&quot;</span><span class="p">:</span> <span class="s2">&quot;, &quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">apps_waiting_migration</span><span class="p">),</span>
                    <span class="p">}</span>
                <span class="p">)</span>
            <span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">stdout</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">style</span><span class="o">.</span><span class="n">NOTICE</span><span class="p">(</span><span class="s2">&quot;Run &#39;python manage.py migrate&#39; to apply them.</span><span class="se">\n</span><span class="s2">&quot;</span><span class="p">))</span>

<div class="viewcode-block" id="BaseCommand.handle"><a class="viewcode-back" href="../../../../howto/custom-management-commands.html#django.core.management.BaseCommand.handle">[docs]</a>    <span class="k">def</span> <span class="nf">handle</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">options</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        The actual logic of the command. Subclasses must implement</span>
<span class="sd">        this method.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">raise</span> <span class="ne">NotImplementedError</span><span class="p">(</span><span class="s1">&#39;subclasses of BaseCommand must provide a handle() method&#39;</span><span class="p">)</span></div></div>


<span class="k">class</span> <span class="nc">AppCommand</span><span class="p">(</span><span class="n">BaseCommand</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    A management command which takes one or more installed application labels</span>
<span class="sd">    as arguments, and does something with each of them.</span>

<span class="sd">    Rather than implementing ``handle()``, subclasses must implement</span>
<span class="sd">    ``handle_app_config()``, which will be called once for each application.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">missing_args_message</span> <span class="o">=</span> <span class="s2">&quot;Enter at least one application label.&quot;</span>

    <span class="k">def</span> <span class="nf">add_arguments</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">parser</span><span class="p">):</span>
        <span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s1">&#39;args&#39;</span><span class="p">,</span> <span class="n">metavar</span><span class="o">=</span><span class="s1">&#39;app_label&#39;</span><span class="p">,</span> <span class="n">nargs</span><span class="o">=</span><span class="s1">&#39;+&#39;</span><span class="p">,</span> <span class="n">help</span><span class="o">=</span><span class="s1">&#39;One or more application label.&#39;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">handle</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">app_labels</span><span class="p">,</span> <span class="o">**</span><span class="n">options</span><span class="p">):</span>
        <span class="kn">from</span> <span class="nn">django.apps</span> <span class="k">import</span> <span class="n">apps</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">app_configs</span> <span class="o">=</span> <span class="p">[</span><span class="n">apps</span><span class="o">.</span><span class="n">get_app_config</span><span class="p">(</span><span class="n">app_label</span><span class="p">)</span> <span class="k">for</span> <span class="n">app_label</span> <span class="ow">in</span> <span class="n">app_labels</span><span class="p">]</span>
        <span class="k">except</span> <span class="p">(</span><span class="ne">LookupError</span><span class="p">,</span> <span class="ne">ImportError</span><span class="p">)</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">CommandError</span><span class="p">(</span><span class="s2">&quot;</span><span class="si">%s</span><span class="s2">. Are you sure your INSTALLED_APPS setting is correct?&quot;</span> <span class="o">%</span> <span class="n">e</span><span class="p">)</span>
        <span class="n">output</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">app_config</span> <span class="ow">in</span> <span class="n">app_configs</span><span class="p">:</span>
            <span class="n">app_output</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">handle_app_config</span><span class="p">(</span><span class="n">app_config</span><span class="p">,</span> <span class="o">**</span><span class="n">options</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">app_output</span><span class="p">:</span>
                <span class="n">output</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">app_output</span><span class="p">)</span>
        <span class="k">return</span> <span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">output</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">handle_app_config</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">app_config</span><span class="p">,</span> <span class="o">**</span><span class="n">options</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Perform the command&#39;s actions for app_config, an AppConfig instance</span>
<span class="sd">        corresponding to an application label given on the command line.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">raise</span> <span class="ne">NotImplementedError</span><span class="p">(</span>
            <span class="s2">&quot;Subclasses of AppCommand must provide&quot;</span>
            <span class="s2">&quot;a handle_app_config() method.&quot;</span><span class="p">)</span>


<span class="k">class</span> <span class="nc">LabelCommand</span><span class="p">(</span><span class="n">BaseCommand</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    A management command which takes one or more arbitrary arguments</span>
<span class="sd">    (labels) on the command line, and does something with each of</span>
<span class="sd">    them.</span>

<span class="sd">    Rather than implementing ``handle()``, subclasses must implement</span>
<span class="sd">    ``handle_label()``, which will be called once for each label.</span>

<span class="sd">    If the arguments should be names of installed applications, use</span>
<span class="sd">    ``AppCommand`` instead.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">label</span> <span class="o">=</span> <span class="s1">&#39;label&#39;</span>
    <span class="n">missing_args_message</span> <span class="o">=</span> <span class="s2">&quot;Enter at least one </span><span class="si">%s</span><span class="s2">.&quot;</span> <span class="o">%</span> <span class="n">label</span>

    <span class="k">def</span> <span class="nf">add_arguments</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">parser</span><span class="p">):</span>
        <span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s1">&#39;args&#39;</span><span class="p">,</span> <span class="n">metavar</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">label</span><span class="p">,</span> <span class="n">nargs</span><span class="o">=</span><span class="s1">&#39;+&#39;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">handle</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">labels</span><span class="p">,</span> <span class="o">**</span><span class="n">options</span><span class="p">):</span>
        <span class="n">output</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">label</span> <span class="ow">in</span> <span class="n">labels</span><span class="p">:</span>
            <span class="n">label_output</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">handle_label</span><span class="p">(</span><span class="n">label</span><span class="p">,</span> <span class="o">**</span><span class="n">options</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">label_output</span><span class="p">:</span>
                <span class="n">output</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">label_output</span><span class="p">)</span>
        <span class="k">return</span> <span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">output</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">handle_label</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">label</span><span class="p">,</span> <span class="o">**</span><span class="n">options</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Perform the command&#39;s actions for ``label``, which will be the</span>
<span class="sd">        string as given on the command line.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">raise</span> <span class="ne">NotImplementedError</span><span class="p">(</span><span class="s1">&#39;subclasses of LabelCommand must provide a handle_label() method&#39;</span><span class="p">)</span>
</pre></div>

          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="../../../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">Mar 04, 2020</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    <a href="../../../index.html" title="Module code" accesskey="U">up</a></div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>